package com.nantai.jiafang.nantaijiafang.nt5s.fragments;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.hzc.imagepicker.SImagePicker;
import com.nantai.jiafang.nantaijiafang.Data;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentUperrorAddBinding;
import com.nantai.jiafang.nantaijiafang.nt5s.Activity.BigImgActivity;
import com.nantai.jiafang.nantaijiafang.nt5s.adapter.PickAdapter;
import com.nantai.jiafang.nantaijiafang.nt5s.fragments.ViewHolder.Holder_Fragment1;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/25.
 */

public class UpErrorfragment_Add extends Fragment {
    FragmentUperrorAddBinding binding;
    private boolean isShowDelete;
    private ArrayList<String> pathlist=new ArrayList<>();
    private ArrayList<String> netpiclist=new ArrayList<>();
    private   PickAdapter pickAdapter;
    private int MODE;
    private UpErrorfragment2 upErrorfragment2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_uperror_add,container,false);
        pickAdapter = new PickAdapter(getContext());
        Holder_Fragment1 holder_fragment1=new Holder_Fragment1(getContext());
        MODE= Data.MODE;
        if (Data.uperror_item!=null) {
            if (Data.uperror_item.getISOK().equals("已处理")) {
                binding.uploaddata.setVisibility(View.GONE);
                binding.selectpics.setVisibility(View.GONE);
            } else {
                if (Data.Selectfragment == 0) {
                    binding.uploaddata.setVisibility(View.GONE);
                    binding.selectpics.setVisibility(View.GONE);
                }
            }
        }
        if (MODE!=-1){
           // Toast.makeText(getContext(), Data.uperror_item.getProDesc(), Toast.LENGTH_SHORT).show();
            upErrorfragment2=Data.upErrorfragment2;
            binding.addProDesc.setText(Data.uperror_item.getProDesc());
            binding.title.setText("修 改");
            binding.uploaddata.setText("修 改");
            holder_fragment1.getimg(Data.uperror_item.getGuid(), new Holder_Fragment1.Download() {
                @Override
                public void onsuccess(ArrayList<String> paths) {
                    pathlist.addAll(paths);
                    netpiclist=paths;
                    Data.netpiclist=paths;
                    //Toast.makeText(getContext(), String.valueOf(paths.size()), Toast.LENGTH_SHORT).show();
                    pickAdapter.setNewData(pathlist);
                }
            });
        }else{
            binding.title.setText("新 增");
            binding.uploaddata.setText("新 增");
            binding.uploaddata.setVisibility(View.VISIBLE);
        }
        binding.addpics.setAdapter(pickAdapter);
        binding.selectpics.setOnClickListener(v -> {
                SImagePicker
                        .from(getActivity())
                        .maxCount(9)
                        .rowCount(5)
                        .showCamera(true)
                        .pickMode(SImagePicker.MODE_IMAGE)
                        .forResult(101);
        });
        binding.addpics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                seeBig(pathlist.get(i));
            }
        });
        binding.addpics.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isShowDelete) {
                    isShowDelete = false;
                } else {
                    isShowDelete = true;
                    pickAdapter.setIsShowDelete(isShowDelete);
                    binding.addpics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            //delete(position);//删除选中项
                            pathlist.remove(position);
                            pickAdapter.removeitem(position);
                            pickAdapter.setIsShowDelete(isShowDelete);
                        }
                    });
                }
                pickAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
                return true;
            }
        });
        binding.uploaddata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Holder_Fragment1 holder_fragment1=new Holder_Fragment1(getContext());
                if (Data.MODE==-1)
                    holder_fragment1.add(binding.addProDesc.getText().toString(),pathlist);
                else {
                    holder_fragment1.modifyy(binding.addProDesc.getText().toString(), pathlist, new Holder_Fragment1.Modifyload() {
                        @Override
                        public void onsuccess() {
                            if (netpiclist.size()!=0){
                                for (String path :
                                        netpiclist) {
                                    new File(path).delete();
                                }
                                upErrorfragment2.onModify(binding.addProDesc.getText().toString(),Data.MODE);
                                netpiclist.clear();
                            }
                            getFragmentManager().popBackStack();
                        }
                    });

                }
            }
        });
        binding.imgBackuperror.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (netpiclist.size()==0){
                    for (String path :
                            netpiclist) {
                        new File(path).delete();
                    }
                }
                getFragmentManager().popBackStack();
            }
        });
        return binding.getRoot();
    }
    //选择图片后返回执行
    public void refresh(ArrayList<String> pathlistt){
        pathlist.addAll(pathlistt);
      //  Toast.makeText(getContext(), pathlist.get(0).toString(), Toast.LENGTH_SHORT).show();
        isShowDelete = false;
        pickAdapter.setIsShowDelete(isShowDelete);
        pickAdapter.setNewData(pathlist);

    }
    public void seeBig(String url) {
        Intent intent = new Intent();
        intent.setClass(getContext(), BigImgActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }

}
